package com.prototipo_picshare;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.prototipo_picshare.Bluetooth.BluetoothSocketListener;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class Prueba_bluetooth extends Activity implements OnClickListener {

	
	Button prueba;
	Button envio_automatico;
	Button serverSoket;
	Button mensajeSoket;
	
	
	String path_file= "/mnt/sdcard/DCIM/Camera/IMG_20121109_134056.jpg";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_prueba_bluetooth);
		
		prueba=(Button) findViewById(R.id.BTprueba_bluetooth);
		prueba.setOnClickListener(this);
	
		envio_automatico=(Button) findViewById(R.id.BTEnvioAutomatioBluetooth);
		envio_automatico.setOnClickListener(this);
		
		serverSoket=(Button) findViewById(R.id.BTSErver_Soket);
		serverSoket.setOnClickListener(this);
		
		mensajeSoket=(Button) findViewById(R.id.BTmensajeSoket);
		mensajeSoket.setOnClickListener(this);
	
		
		BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
		
		if (btAdapter == null) {
		 // Device does not support Bluetooth
		 // Inform user that we're done.     	
		}
		
	
	}

	

	private void activarBT() {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_SEND);
		intent.setType("text/plain");
		File archivo=new File(path_file);
		intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(archivo) );
		///////////////////pakage manager
		PackageManager pm = getPackageManager();
		List<ResolveInfo> appsList = pm.queryIntentActivities( intent, 0);
		 
		if(appsList.size() > 0) {
			//Toast.makeText(this,"su telefono no cuenta con aplicacion de intercambio de datos",Toast.LENGTH_LONG).show();
		}
		//selleccionar la aplicacion de bluetooth
		String packageName = null;
		String className = null;
		boolean found = false;
		// BluetoothAdapter.checkBluetoothAddress("");
		for(ResolveInfo info: appsList){
			
		  packageName = info.activityInfo.packageName;
		  if( packageName.equals("com.android.bluetooth")){
		     className = info.activityInfo.name;
		     found = true;
		     break;// found
		  }
		  
		}
		if(! found){
		  Toast.makeText(this,"...",
		                 Toast.LENGTH_SHORT).show();
		  // exit
		}
		
		intent.setClassName(packageName, className);
		startActivity(intent);
		
		
	}
	
	private void envio_automatico() {
		// TODO Auto-generated method stub
		String mdeviceadd="94:71:AC:39:D2:1F";
		File myFile = new File(path_file);
		//BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
			if(mBluetoothAdapter == null)
		                 {

		                 }

		                 if(!mBluetoothAdapter.isEnabled())
		                 {
		                     Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		                     startActivityForResult(enableBluetooth, 0);
		                 }
		                 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		                 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
		                 if(pairedDevices.size() > 0)
		                 {
		                     for(BluetoothDevice device : pairedDevices)
		                     {
		                    	 
		                    	 if(device.getName()!=null) 
		                         { 
		                        	 String nombreDispocitivo=device.getName();
			                    	 Toast.makeText(this,nombreDispocitivo,Toast.LENGTH_LONG).show(); 
		                         
		                    	 
			                         if(device.getName().equals("chota")) 
			                         {
			                        	
			                             BluetoothDevice mmDevice = device;
			                             mdeviceadd = device.getAddress();
			                             Toast.makeText(this,nombreDispocitivo,Toast.LENGTH_LONG).show(); 
			                             //Log.d(TAG, "found device");
			                             break;
				                         
			                         }
			                         else {
	
			                             //Log.d(TAG, "FAilure");
			                         }
		                         }
		                     }
		                 }
		                 ContentValues values = new ContentValues();
		                 values.put(BluetoothShare.URI,Environment.getExternalStorageDirectory()+"/mysdfile.txt");
		                 values.put(BluetoothShare.DESTINATION,   mdeviceadd);
		                 values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
		                 Long ts = System.currentTimeMillis();
		                 values.put(BluetoothShare.TIMESTAMP, ts);
		                 getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
	}
	


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.prueba_bluetooth, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
			case R.id.BTprueba_bluetooth:
				activarBT();
				Toast.makeText(this,"intercambiando archivo",Toast.LENGTH_LONG).show();
			break;
			
			case R.id.BTEnvioAutomatioBluetooth:
				envio_automatico();
			break;
			
			case R.id.BTSErver_Soket:
			/*	
			 * BluetoothSocketListener bsl = new BluetoothSocketListener(socket, handler, textView)
			    Thread messageListener = new Thread(bsl);
			    messageListener.start();
			*/
			break;

			
			default:
			
			break;
		}
		
	}


	

}
